<template>
  <view style="position: relative">
    <view
      v-show="showImage"
      class="verify-image-out"
    >
      <view
        class="verify-image-panel"
        :style="{'width': imgSize.width, 'height': imgSize.height, 'margin-bottom': vSpace + 'px'}"
      >
        <view
          v-show="showRefresh"
          class="verify-refresh"
          style="z-index:3"
          @click="refresh"
        >
          <text class="iconfont icon-refresh" />
        </view>
        <image
          id="image"
          ref="canvasRef"
          :src="pointBackImgBase?('data:image/png;base64,'+pointBackImgBase):defaultImg"
          style="width:100%;height:100%;display:block"
          @click=" bindingClick? canvasClick($event): undefined"
        />
        <view
          v-for="(tempPoint, index) in tempPoints"
          :key="index"
          class="point-area"
          :style="{
            'background-color':'#1abd6c',
            color:'#fff',
            'z-index':9999,
            width:'20px',
            height:'20px',
            'text-align':'center',
            'line-height':'20px',
            'border-radius': '50%',
            position:'absolute',
            top:parseInt(tempPoint.y-10) + 'px',
            left:parseInt(tempPoint.x-10) + 'px'
          }"
        >
          {{ index + 1 }}
        </view>
      </view>
    </view>
    <!-- 'height': barSize.height, -->
    <view
      class="verify-bar-area"
      :style="{'width': imgSize.width, 'color': barAreaColor, 'border-color': barAreaBorderColor, 'line-height':'40px'}"
    >
      <text class="verify-msg">
        {{ text }}
      </text>
    </view>
  </view>
</template>
<script type="text/babel" setup>
/**
     * VerifyPoints
     * @description 点选
     * */
import { watch } from 'vue'
import { aesEncrypt } from '../utils/ase.js'
import { reqGet, reqCheck } from '../utils/request.js'

const props = defineProps({
  // 弹出式pop，固定fixed
  mode: {
    type: String,
    default: 'fixed'
  },
  captchaType: {
    type: String,
    default: '1'
  },
  // 间隔
  vSpace: {
    type: Number,
    default: 5
  },
  imgSize: {
    type: Object,
    default () {
      return {
        width: '310px',
        height: '155px'
      }
    }
  },
  barSize: {
    type: Object,
    default () {
      return {
        width: '310px',
        height: '40px'
      }
    }
  },
  defaultImg: {
    type: String,
    default: ''
  }
})
const { proxy } = getCurrentInstance()
let secretKey = '' // 后端返回的加密秘钥 字段
const checkNum = 3 //
const fontPos = [] // 选中的坐标信息
let checkPosArr = [] // 用户点击的坐标
let num = 1 // 点击的记数
const pointBackImgBase = ref('') // 后端获取到的背景图片
let poinTextList = [] // 后端返回的点击字体顺序
let backToken = '' // 后端返回的token值
const imgRand = ref(0) // 随机的背景图片
const setSize = reactive({
  imgHeight: 0,
  imgWidth: 0,
  barHeight: 0,
  barWidth: 0
})
const showImage = ref(true)
const tempPoints = ref([])
const text = ref('')
const barAreaColor = ref('#fff')
const barAreaBorderColor = ref('#fff')
const showRefresh = ref(true)
const bindingClick = ref(true)
let imgLeft = ''
let imgTop = ''

const canvasRef = ref(null)
const canvasClick = (e) => {
  const query = uni.createSelectorQuery().in(proxy)
  query.select('#image').boundingClientRect(data => {
    imgLeft = Math.ceil(data.left)
    imgTop = Math.ceil(data.top)
    checkPosArr.push(getMousePos(canvasRef.value, e))
    if (num == checkNum) {
      num = createPoint(getMousePos(canvasRef.value, e))
      // 按比例转换坐标值
      checkPosArr = pointTransfrom(checkPosArr, props.captchaType)
      // 等创建坐标执行完
      setTimeout(() => {
        // 发送后端请求
        const captchaVerification = secretKey ? aesEncrypt(backToken + '---' + JSON.stringify(checkPosArr), secretKey) : backToken + '---' + JSON.stringify(checkPosArr)
        const data = {
          captchaType: props.captchaType,
          pointJson: secretKey ? aesEncrypt(JSON.stringify(checkPosArr), secretKey) : JSON.stringify(checkPosArr),
          token: backToken
        }
        const params = {
          data
        }
        reqCheck(params).then(({ data: res }) => {
          if (res.repCode == '0000') {
            barAreaColor.value = '#4cae4c'
            barAreaBorderColor.value = '#5cb85c'
            text.value = '验证成功'
            bindingClick.value = false
            setTimeout(() => {
              if (props.mode == 'pop') {
                proxy.$parent.clickShow = false
              }
              refresh()
            }, 1500)
            proxy.$parent.$emit('success', { captchaVerification })
          } else {
            proxy.$parent.$emit('error', proxy)
            barAreaColor.value = '#d9534f'
            barAreaBorderColor.value = '#d9534f'
            text.value = '验证失败'
            setTimeout(() => {
              refresh()
            }, 700)
          }
        })
      }, 400)
    }
    if (num < checkNum) {
      num = createPoint(getMousePos(canvasRef.value, e))
    }
  }).exec()
}
// 获取坐标
// eslint-disable-next-line no-unused-vars
const getMousePos = (obj, e) => {
  const position = {
    x: Math.ceil(e.detail.x) - imgLeft,
    y: Math.ceil(e.detail.y) - imgTop
  }
  return position
}
// 创建坐标点
const createPoint = (pos) => {
  tempPoints.value.push(Object.assign({}, pos))
  return ++num
}
const refresh = () => {
  tempPoints.value.splice(0, tempPoints.value.length)
  barAreaColor.value = '#000'
  barAreaBorderColor.value = '#ddd'
  bindingClick.value = true

  fontPos.splice(0, fontPos.length)
  checkPosArr.splice(0, checkPosArr.length)
  num = 1

  getPictrue()

  // text = '验证失败'
  showRefresh.value = true
}
// 请求背景图片和验证图片
const getPictrue = () => {
  const data = {
    captchaType: props.captchaType,
    clientUid: uni.getStorageSync('bbcPoint'),
    ts: Date.now() // 现在的时间戳
  }
  const params = {
    data
  }
  reqGet(params).then(({ data: res }) => {
    if (res.repCode == '0000') {
      pointBackImgBase.value = res.repData.originalImageBase64
      backToken = res.repData.token
      secretKey = res.repData.secretKey
      poinTextList = res.repData.wordList
      text.value = '请依次点击【' + poinTextList.join(',') + '】'
    }
    // 判断接口请求次数是否失效
    if (res.repCode == '6201') {
      pointBackImgBase.value = null
    }
  })
}
// 坐标转换函数
const pointTransfrom = (pointArr, imgSize) => {
  const newPointArr = pointArr.map(p => {
    const x = Math.round(310 * p.x / parseInt(imgSize.width))
    const y = Math.round(155 * p.y / parseInt(imgSize.height))
    return { x, y }
  })
  // console.log(newPointArr,"newPointArr");
  return newPointArr
}
const init = () => {
  // 加载页面
  fontPos.splice(0, fontPos.length)
  checkPosArr.splice(0, checkPosArr.length)
  num = 1
  nextTick(() => {
    refresh()
    proxy.$parent.$emit('ready', proxy)
  })
}
// type变化则全面刷新
watch(() => props.type, () => {
  init()
}, {
  immediate: true
})
defineExpose({
  imgRand,
  setSize
})
</script>

<style lang="scss" scoped>
 //eslint-disable vue-scoped-css/no-unused-selector
    .verifybox{
        position: relative;
        box-sizing: border-box;
        border-radius: 2px;
        border: 1px solid #e4e7eb;
        background-color: #fff;
        box-shadow: 0 0 10px rgba(0,0,0,.3);
        left: 50%;
        top:50%;
        transform: translate(-50%,-50%);
    }
    .verifybox-top{
        padding: 0 15px;
        height: 50px;
        line-height: 50px;
        text-align: left;
        font-size: 16px;
        color: #45494c;
        border-bottom: 1px solid #e4e7eb;
        box-sizing: border-box;
    }
    .verifybox-bottom{
        /* padding: 15px; */
        box-sizing: border-box;
    }
    .verifybox-close{
        position: absolute;
        top: 13px;
        right: 9px;
        width: 24px;
        height: 24px;
        text-align: center;
        cursor: pointer;
    }
    .mask{
        position: fixed;
        top: 0;
        left:0;
        z-index: 1001;
        width: 100%;
        height: 100vh;
        background: rgba(0,0,0,.3);
        /* display: none; */
        transition: all .5s;
    }
    .verify-tips{
        position: absolute;
        left: 0px;
        bottom:0px;
        width: 100%;
        height: 30px;
        background-color:rgb(231, 27, 27,.5);
        line-height:30px;
        color: #fff;
    }
    .tips-enter,.tips-leave-to{
        bottom: -30px;
    }
    .tips-enter-active,.tips-leave-active{
        transition: bottom .5s;
    }
    /* ---------------------------- */
    /*常规验证码*/
    .verify-code {
        font-size: 20px;
        text-align: center;
        cursor: pointer;
        margin-bottom: 5px;
        border: 1px solid #ddd;
    }

    .cerify-code-panel {
        height: 100%;
        overflow: hidden;
    }

    .verify-code-area {
        float: left;
    }

    .verify-input-area {
        float: left;
        width: 60%;
        padding-right: 10px;

    }

    .verify-change-area {
        line-height: 30px;
        float: left;
    }

    .varify-input-code {
        display: inline-block;
        width: 100%;
        height: 25px;
    }

    .verify-change-code {
        color: #337AB7;
        cursor: pointer;
    }

    .verify-btn {
        width: 200px;
        height: 30px;
        background-color: #337AB7;
        color: #FFFFFF;
        border: none;
        margin-top: 10px;
    }

    /*滑动验证码*/
    .verify-bar-area {
        position: relative;
        background: #FFFFFF;
        text-align: center;
        -webkit-box-sizing: content-box;
        -moz-box-sizing: content-box;
        box-sizing: content-box;
        border: 1px solid #ddd;
        -webkit-border-radius: 4px;
    }

    .verify-bar-area .verify-move-block {
        position: absolute;
        top: 0px;
        left: 0;
        background: #fff;
        cursor: pointer;
        -webkit-box-sizing: content-box;
        -moz-box-sizing: content-box;
        box-sizing: content-box;
        box-shadow: 0 0 2px #888888;
        -webkit-border-radius: 1px;
    }

    .verify-bar-area .verify-move-block:hover {
        background-color: #337ab7;
        color: #FFFFFF;
    }

    .verify-bar-area .verify-left-bar {
        position: absolute;
        top: -1px;
        left: -1px;
        background: rgb(8, 43, 166, .3);
        cursor: pointer;
        -webkit-box-sizing: content-box;
        -moz-box-sizing: content-box;
        box-sizing: content-box;
        border: 1px solid #ddd;
    }

    .verify-image-panel {
        margin: 0;
        -webkit-box-sizing: content-box;
        -moz-box-sizing: content-box;
        box-sizing: content-box;
        border-top: 1px solid #ddd;
        border-bottom: 1px solid #ddd;
        border-radius: 3px;
        position: relative;
    }

    .verify-image-panel .verify-refresh {
        width: 25px;
        height: 25px;
        text-align: center;
        padding: 5px;
        cursor: pointer;
        position: absolute;
        top: 0;
        right: 0;
        z-index: 2;
    }

    .verify-image-panel .icon-refresh {
        font-size: 20px;
        color: #fff;
    }

    .verify-image-panel .verify-gap {
        background-color: #fff;
        position: relative;
        z-index: 2;
        border: 1px solid #fff;
    }

    .verify-bar-area .verify-move-block .verify-sub-block {
        position: absolute;
        text-align: center;
        z-index: 3;
        /* border: 1px solid #fff; */
    }

    .verify-bar-area .verify-move-block .verify-icon {
        font-size: 18px;
    }

    .verify-bar-area .verify-msg {
        z-index: 3;
    }

    /*字体图标的css*/
    /*@font-face {font-family: "iconfont";*/
    /*src: url('../fonts/iconfont.eot?t=1508229193188'); !* IE9*!*/
    /*src: url('../fonts/iconfont.eot?t=1508229193188#iefix') format('embedded-opentype'), !* IE6-IE8 *!*/
    /*url('data:application/x-font-woff;charset=utf-8;base64,d09GRgABAAAAAAaAAAsAAAAACUwAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABHU1V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4g4EBAJ79CeQ=') format('woff'),*/
    /*url('../fonts/iconfont.ttf?t=1508229193188') format('truetype'), !* chrome, firefox, opera, Safari, Android, iOS 4.2+*!*/
    /*url('../fonts/iconfont.svg?t=1508229193188#iconfont') format('svg'); !* iOS 4.1- *!*/
    /*}*/

    .iconfont {
        font-family: "iconfont" !important;
        font-size: 16px;
        font-style: normal;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }

    .icon-check:before {
        content: " ";
        display: block;
        width: 16px;
        height: 16px;
        position: absolute;
        margin: auto;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        z-index: 9999;
        background-image: url("");
        background-size: contain;
    }

    .icon-close:before {
        content: " ";
        display: block;
        width: 16px;
        height: 16px;
        position: absolute;
        margin: auto;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        z-index: 9999;
        background-image: url("");
        background-size: contain;
    }

    .icon-right:before {
        content: " ";
        display: block;
        width: 22px;
        height: 22px;
        position: absolute;
        margin: auto;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        background-size: cover;
        z-index: 9999;
        background-image: url("");
        background-size: contain;
    }

    .icon-refresh:before {
        content: " ";
        display: block;
        width: 16px;
        height: 16px;
        position: absolute;
        margin: auto;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        z-index: 9999;
        background-image: url("");
        background-size: contain;
    }
</style>
